let lastEvent = null
const arr = ['click', 'touchstart', 'mousedown', 'keydown', 'mouseover']
arr.forEach(eventType => {
  document.addEventListener(eventType, (event) => {
    lastEvent = event
  }, {
    // 捕获阶段
    capture: true,
    // 不阻止默认事件
    passive: true,
  })
})

// 获取最后一次交互事件的event
const getLastEvent = () => {
  return lastEvent
}

// 获取最后一次交互事件触发的dom元素
const getLastElement = () => {
  let lastElement = ''
  if (lastEvent && lastEvent.path) {
    lastElement = lastEvent.path.reverse()
    // window document没有nodeName
    .filter(element => (element !== document && element !== window))
    .map(element => {
      if (element.id) {
        return `${element.nodeName.toLowerCase()}#${element.id}`
      } else if (element.className && typeof element.className === 'string') {
        return `${element.nodeName.toLowerCase()}.${element.className}`
      } else {
        return `${element.nodeName.toLowerCase()}`
      }
    }).join(' > ')
  }
  return lastElement
}
  
export { getLastEvent, getLastElement }